"""添加统计分析相关模型表

Revision ID: 1417e9df65f1
Revises: 03678fe142c6
Create Date: 2025-09-13 14:06:56.228451

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import sqlite

# revision identifiers, used by Alembic.
revision: str = '1417e9df65f1'
down_revision: Union[str, Sequence[str], None] = '03678fe142c6'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_service_config_templates_id'), table_name='service_config_templates')
    op.drop_table('service_config_templates')
    op.drop_table('service_usage_logs')
    op.drop_table('service_classes')
    op.drop_table('pollution_type_examples')
    op.drop_table('pollution_type_relations')
    op.drop_table('pollution_types')
    op.drop_table('service_instances')
    op.create_index(op.f('ix_analysis_task_id'), 'analysis_task', ['id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_analysis_task_id'), table_name='analysis_task')
    op.create_table('service_instances',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('service_class_id', sa.INTEGER(), nullable=False),
    sa.Column('user_id', sa.INTEGER(), nullable=True),
    sa.Column('instance_name', sa.VARCHAR(length=100), nullable=False),
    sa.Column('instance_config', sqlite.JSON(), nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), nullable=True),
    sa.Column('status', sa.VARCHAR(length=20), nullable=True),
    sa.Column('usage_count', sa.INTEGER(), nullable=True),
    sa.Column('last_used_at', sa.DATETIME(), nullable=True),
    sa.Column('created_at', sa.DATETIME(), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), nullable=True),
    sa.Column('is_deleted', sa.BOOLEAN(), nullable=True),
    sa.ForeignKeyConstraint(['service_class_id'], ['service_classes.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('pollution_types',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('customer_id', sa.VARCHAR(length=50), nullable=False),
    sa.Column('code', sa.VARCHAR(length=20), nullable=False),
    sa.Column('name', sa.VARCHAR(length=100), nullable=False),
    sa.Column('parent_code', sa.VARCHAR(length=20), nullable=True),
    sa.Column('level', sa.INTEGER(), nullable=True),
    sa.Column('tag_name', sa.VARCHAR(length=100), nullable=True),
    sa.Column('tag_category', sa.VARCHAR(length=50), nullable=True),
    sa.Column('usage_scope', sa.TEXT(), nullable=True),
    sa.Column('exclusion_scope', sa.TEXT(), nullable=True),
    sa.Column('display_name', sa.VARCHAR(length=100), nullable=True),
    sa.Column('parent_tag_code', sa.VARCHAR(length=20), nullable=True),
    sa.Column('sort_order', sa.INTEGER(), nullable=True),
    sa.Column('description', sa.TEXT(), nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), nullable=True),
    sa.Column('is_system', sa.BOOLEAN(), nullable=True),
    sa.Column('created_at', sa.DATETIME(), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), nullable=True),
    sa.Column('is_deleted', sa.BOOLEAN(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('pollution_type_relations',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('customer_id', sa.VARCHAR(length=50), nullable=False),
    sa.Column('parent_type_id', sa.INTEGER(), nullable=False),
    sa.Column('child_type_id', sa.INTEGER(), nullable=False),
    sa.Column('relation_type', sa.VARCHAR(length=20), nullable=True),
    sa.Column('created_at', sa.DATETIME(), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), nullable=True),
    sa.Column('is_deleted', sa.BOOLEAN(), nullable=True),
    sa.ForeignKeyConstraint(['child_type_id'], ['pollution_types.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['parent_type_id'], ['pollution_types.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('pollution_type_examples',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('customer_id', sa.VARCHAR(length=50), nullable=False),
    sa.Column('pollution_type_id', sa.INTEGER(), nullable=False),
    sa.Column('example_content', sa.TEXT(), nullable=False),
    sa.Column('judgment_basis', sa.TEXT(), nullable=True),
    sa.Column('keywords', sa.TEXT(), nullable=True),
    sa.Column('confidence_level', sa.FLOAT(), nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), nullable=True),
    sa.Column('created_at', sa.DATETIME(), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), nullable=True),
    sa.Column('is_deleted', sa.BOOLEAN(), nullable=True),
    sa.ForeignKeyConstraint(['pollution_type_id'], ['pollution_types.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('service_classes',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('service_id', sa.VARCHAR(length=50), nullable=False),
    sa.Column('name', sa.VARCHAR(length=100), nullable=False),
    sa.Column('description', sa.TEXT(), nullable=True),
    sa.Column('category', sa.VARCHAR(length=20), nullable=True),
    sa.Column('provider', sa.VARCHAR(length=20), nullable=True),
    sa.Column('model_code', sa.VARCHAR(length=100), nullable=True),
    sa.Column('max_tokens', sa.INTEGER(), nullable=True),
    sa.Column('temperature', sa.FLOAT(), nullable=True),
    sa.Column('prompt_template_id', sa.INTEGER(), nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), nullable=True),
    sa.Column('config', sqlite.JSON(), nullable=True),
    sa.Column('usage_count', sa.INTEGER(), nullable=True),
    sa.Column('last_used_at', sa.DATETIME(), nullable=True),
    sa.Column('status', sa.VARCHAR(length=20), nullable=True),
    sa.Column('error_message', sa.TEXT(), nullable=True),
    sa.Column('created_at', sa.DATETIME(), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), nullable=True),
    sa.Column('is_deleted', sa.BOOLEAN(), nullable=True),
    sa.ForeignKeyConstraint(['prompt_template_id'], ['prompt_templates.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('service_id')
    )
    op.create_table('service_usage_logs',
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('service_class_id', sa.INTEGER(), nullable=False),
    sa.Column('service_instance_id', sa.INTEGER(), nullable=True),
    sa.Column('user_id', sa.INTEGER(), nullable=True),
    sa.Column('request_data', sqlite.JSON(), nullable=True),
    sa.Column('response_data', sqlite.JSON(), nullable=True),
    sa.Column('execution_time', sa.INTEGER(), nullable=True),
    sa.Column('token_count', sa.INTEGER(), nullable=True),
    sa.Column('cost', sa.VARCHAR(length=50), nullable=True),
    sa.Column('status', sa.VARCHAR(length=20), nullable=True),
    sa.Column('error_message', sa.TEXT(), nullable=True),
    sa.Column('created_at', sa.DATETIME(), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), nullable=True),
    sa.Column('is_deleted', sa.BOOLEAN(), nullable=True),
    sa.ForeignKeyConstraint(['service_class_id'], ['service_classes.id'], ),
    sa.ForeignKeyConstraint(['service_instance_id'], ['service_instances.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('service_config_templates',
    sa.Column('name', sa.VARCHAR(length=100), nullable=False),
    sa.Column('description', sa.TEXT(), nullable=True),
    sa.Column('service_category', sa.VARCHAR(length=20), nullable=True),
    sa.Column('config_schema', sqlite.JSON(), nullable=True),
    sa.Column('default_config', sqlite.JSON(), nullable=True),
    sa.Column('usage_count', sa.INTEGER(), nullable=True),
    sa.Column('created_at', sa.DATETIME(), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), nullable=True),
    sa.Column('id', sa.INTEGER(), nullable=False),
    sa.Column('is_deleted', sa.BOOLEAN(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_service_config_templates_id'), 'service_config_templates', ['id'], unique=False)
    # ### end Alembic commands ###
